import {Spin} from 'iview'

export default {
    operatorRequestHandler(response, successFn, errorFn, successTip = '操作成功', showLoading = true, stream = false) {
        //增删改结果处理
        let loadingId = null;
        showLoading && (loadingId = this.loadingCreate());
        response.then(res => {
            if (stream) {
                return res.data;
            } else {
                if (res.data.code !== 0) {
                    throw new Error(res.data.message);
                } else {
                    return res.data;
                }
            }
        }).then(data => {
            //成功回调提示
            successTip && this.$Message.success(successTip);
            
            //成功回调函数
            successFn && successFn(data, response);
            showLoading && this.loadingHideById(loadingId);
        }).catch(err => {
            errorFn && errorFn(response);
            console.log(err);
            this.$Message.error(err.toString());
            showLoading && this.loadingHideById(loadingId);
        });
    },
    pageRequestHandler(response, successFn, errorFn, showLoading = true) {
        //获取页面数据
        let loadingId = null;
        showLoading && (loadingId = this.loadingCreate());
        
        response.then(res => {
            if (res.data.code !== 0) {
                this.$Message.error(res.data.message);
                
                throw new Error(res.data.message);
            } else {
                return res.data.data;
            }
        }).then(data => {
            //获取列表数据
            successFn && successFn(data);
            showLoading && this.loadingHideById(loadingId);
        }).catch(err => {
            errorFn && errorFn(err);
            console.log(err);
            this.$Message.error(err.toString());
            showLoading && this.loadingHideById(loadingId);
        });
    },
    loadingShow() {//加载中提示展示
        this.$Spin.show({
            render: (h) => {
                return h('div', [
                    h('Icon', {
                        'class': 'demo-spin-icon-load',
                        props  : {
                            type: 'ios-loading',
                            size: 36
                        }
                    }),
                ])
            }
        });
    },
    loadingHide(delay) {//加载中提示关闭
        setTimeout(() => {
            this.$Spin.hide();
        }, delay ? delay : 10);
    },
    popConfirm(okFn, title, content, cancelFn, type = 'confirm', width = 416) { //type: info, success, warning, error, confirm
        this.$Modal[type]({
            title   : title ? title : '删除确认',
            content : content ? content : '确认删除该条目吗？',
            width   : width,
            onOk    : okFn,
            onCancel: cancelFn ? cancelFn : () => {}
        })
    },
    
    //独立loading生成相关
    loadingList: {},//加载中对象暂存对象
    loadingHideById(popId) {//加载中的隐藏
        if (popId) {
            //隐藏对应id遮罩
            this.loadingList[popId] && this.loadingList[popId].remove && this.loadingList[popId].remove(() => {});
            delete this.loadingList[popId];
        }
    },
    loadingCreate() {//生成loading
        var loadingPopId               = Date.now() + Math.random().toString();
        this.loadingList[loadingPopId] = Spin.newInstance({
            // render: (h) => {
            //     return h('div', [
            //         h('Icon', {
            //             'class': 'demo-spin-icon-load',
            //             props  : {
            //                 type: 'ios-loading',
            //                 size: 36
            //             }
            //         }),
            //     ])
            // }
        });
        this.loadingList[loadingPopId].show();
        return loadingPopId;
    },
};
